package dao;

import datacontainers.Category;
import datacontainers.Dish;
import datacontainers.OrderItem;

public class TextParser {

	public static Dish parseDish(String str) {
		int id = 0;
		int cat;
		String name;
		String description;
		Double cost = 0.0;

		int p1 = 0;
		int p2 = 0;

		while (str.charAt(p2) != '\\') {
			p2++;
		}

		try {
			id = Integer.parseInt(str.substring(p1, p2));
		} catch (NumberFormatException e) {
			// bad string
		}

		p1 = p2;//
		p2++;

		while (str.charAt(p2) != '\\') {
			p2++;
		}
		cat = Integer.parseInt(str.substring(p1 + 1, p2));

		p1 = p2;//
		p2++;

		while (str.charAt(p2) != '\\') {
			p2++;
		}
		name = str.substring(p1 + 1, p2);

		p1 = p2;//
		p2++;

		while (str.charAt(p2) != '\\') {
			p2++;
		}
		description = str.substring(p1 + 1, p2);

		p1 = p2;//
		p2++;

		// while (str.charAt(p2)!='\\') {p2++;}

		try {
			cost = Double.parseDouble(str.substring(p1 + 1, str.length() - 1));
		} catch (NumberFormatException e) {
			// /
		}

		return new Dish(id, Category.values()[cat], name, description, cost);
	}

	public static OrderItem parseOI(String str) {
		int id = 0;
		int count = 0;

		int p1 = 0;
		int p2 = 0;

		while (str.charAt(p2) != '\\') {
			p2++;
		}

		try {
			id = Integer.parseInt(str.substring(p1, p2));
		} catch (NumberFormatException e) {
			// bad string
		}

		p1 = p2;//

		try {
			count = Integer.parseInt(str.substring(p1 + 1, str.length() - 1));
		} catch (NumberFormatException e) {
			// bad string
		}

		Dish d = new Dish(id, null, null, null, 0.0);

		return new OrderItem(d, count);
	}
}
